package com.wangtao.nio;

import javafx.scene.layout.VBox;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel01 {

    public static void main(String[] args) throws IOException {
        String str = "hello nio 我是";
        FileOutputStream fos = new FileOutputStream(new File("D:\\a.txt"));
        FileChannel fosChannel = fos.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put(str.getBytes());
        buffer.flip();
        //将数据从缓冲区写到通道
        fosChannel.write(buffer);
        fos.close();

        FileInputStream fi = new FileInputStream(new File("D:\\a.txt"));
        FileChannel fiChannel = fi.getChannel();
        //将数据从通道读到缓冲区
        fiChannel.read(buffer);
        System.out.println(new String(buffer.array()));
        fi.close();

    }
}
